import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterdemo/0demo/appbar_tab_bar/appbar_tab_bar.dart';
import 'package:flutterdemo/0demo/database/database_test.dart';
import 'package:flutterdemo/0demo/drop_down_widget/drop_down_widget.dart';
import 'package:flutterdemo/0demo/img/gallery_photo_view.dart';
import 'package:flutterdemo/0demo/lifecycle/TestLifeCyclePage.dart';
import 'package:flutterdemo/0demo/local_notification/local_notification_test_page.dart';
import 'package:flutterdemo/0demo/module_test/module_test.dart';
import 'package:flutterdemo/0demo/native/native_view_page.dart';
import 'package:flutterdemo/0demo/navigator/splash_page.dart';
import 'package:flutterdemo/0demo/notifier/notifier_test_pages.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/provider_test_pages1.dart';
import 'package:flutterdemo/0demo/provider/test_provider_list/test_provider_list.dart';
import 'package:flutterdemo/0demo/provider/top_provider/test_top_page.dart';
import 'package:flutterdemo/0demo/route/fade_route.dart';
import 'package:flutterdemo/0demo/ui/bottom_navigation/test_bottom_navigator_page.dart';
import 'package:flutterdemo/0demo/ui/chat_refresh/chat_refresh_page.dart';
import 'package:flutterdemo/0demo/ui/custom_charts/custom_chat_test_page.dart';
import 'package:flutterdemo/0demo/ui/dismissable/dissmissible_widget_page.dart';
import 'package:flutterdemo/0demo/ui/dismissable/slide_remove/slide_remove_page.dart';
import 'package:flutterdemo/0demo/ui/gesture/drag_widget.dart';
import 'package:flutterdemo/0demo/ui/ui_page.dart';
import 'package:flutterdemo/0demo/utils/bus_test_page.dart';
import 'package:provider/provider.dart';

import 'bloc/bloc/counter_page.dart';
import 'common/common.dart';
import 'download/test_download_page.dart';
import 'multi_image_picker/image_picker.dart';
import 'music_vibrate/test_music_vibrate.dart';
import 'native/NativePluginPage.dart';
import 'provider/inherited_widget_test/inherited_widget_test_page.dart';
import 'provider/test_provider_list/test_provider_list1.dart';
import 'provider/top_provider/top_provider.dart';
import 'route/page_route_manager.dart';
import 'route/route_params.dart';
import 'route/routes_name.dart';
import 'route/test_future_build.dart';
import 'ui/scroll_offset_test/scroll_offset_test_page.dart';
import 'ui/test_keep_alive_widget_page.dart';

// flutter 面试：
// https://github.com/ahyangnb/flutter_interview
@deprecated
// see ../../main.dart!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void main() {
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => TopProvider()),
    ],
    child: MaterialApp(
        home: MainPage(),
        // routes: routePath,
        initialRoute: RoutesName.MAIN_PAGE,
        onGenerateRoute: (RouteSettings settings) {
          return pageRoute(settings);
        }),
  ));
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    _tabController = TabController(length: tabs.length, vsync: this);
    _tabController.addListener(() {
      print("index == " + _tabController.index.toString());
    });

    // 设置图片缓存策略
    // 最多2000张
    PaintingBinding.instance.imageCache.maximumSize = 2000;
    // 最大200M，左移20位，200*2^20
    PaintingBinding.instance.imageCache.maximumSizeBytes = 200 << 20;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double scw = MediaQuery.of(context).size.width;
    double sch = MediaQuery.of(context).size.height;
    double scw1 = window.physicalSize.width / window.devicePixelRatio;
    double sch1 = window.physicalSize.height / window.devicePixelRatio;
    debugPrint("scw == $scw, sch == $sch, scw1 == $scw1, sch1 == $sch1");
    ScreenUtil.init(context, width: 1080, height: 1920, allowFontScaling: false);
    setStatusBarWhite(); // 设置沉浸式状态栏并修改状态栏颜色，对应scaffold brightness
    return Scaffold(
      appBar: AppBar(
        title: Text("demo"),
      ),
      body: DemoList(),
    );
  }
}

class DemoList extends StatefulWidget {
  @override
  _DemoListState createState() => _DemoListState();
}

class _DemoListState extends State<DemoList> with WidgetsBindingObserver, RouteAware {
  List<String> _images = List.of({
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592544308075&di"
        "=e386fb8a75ea743ee6eb05ed2b58edb6&imgtype=0&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2334987719%2C72148412%26fm%3D214%26gp%3D0.jpg",
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592544307346&di"
        "=1a973f7436412b2110d902c3c8cf7afe&imgtype=0&src=http%3A%2F%2Ffile02.16sucai.com%2Fd%2Ffile%2F2014%2F0419%2Fd9f4710e211cd8bce6b8ef361b805fd3.jpg",
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592544307345&di"
        "=688a19fce9b68e3b2bc779f097deabb7&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fd1160924ab18972b48067763e4cd7b899e510a5e.jpg",
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592544307345&di"
        "=27680241f5167597570eb3d6aee424cf&imgtype=0&src=http%3A%2F%2Ffile01.16sucai.com%2Fd%2Ffile%2F2013%2F0528%2F20130528100729502.jpg",
  });

  @override
  void didPush() {
    print("------> didPush");
    super.didPush();
  }

  @override
  void didPop() {
    print("------> didPop");
    super.didPop();
  }

  @override
  void didPopNext() {
    print("------> didPopNext");
    super.didPopNext();
  }

  @override
  void didPushNext() {
    print("------> didPushNext");
    super.didPushNext();
  }

  @override
  void deactivate() {
    print("deactivate");
    super.deactivate();
  }

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print("lifeChanged $state");
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => AppbarTabBarPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("appbar tabbar"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => DropDownWidgetPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("drop down widget"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context,
              CupertinoPageRoute(
                  builder: (context) => GalleryPhotoViewPage(
                        initialPageIndex: 0,
                        images: _images,
                      ))),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("gallery photo view"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => DatabasePage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("database"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(context, CupertinoPageRoute(builder: (context) => UIPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("ui"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => TestKeepAliveWidgetPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("test keep alive widget list view"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => LifeCyclePage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("test lifecycle"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => DownloadPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("download and permission file storage"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => NativePluginPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("native plugin too complex, use above way(download and permission ..)"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => TestBottomNavigatorPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("bottom navigation"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => ChatRefreshPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("chat page refresh reverse"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => DismissibleWidgetPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("dissmissible widget"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => SlideRemovePage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("slide remove widget"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => ImagePickerTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("multi sigle image picker test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => ProviderTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("provider test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => CounterPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("bloc test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => NativeViewPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("native view"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => LocalNotificationTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("local notification"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => BusTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("eventbus test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => DragWidget())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("drag widget"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => ModuleTest())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("module test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => SplashPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("navigator test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => TestProviderList())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("TestProviderList"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => TestProviderList1())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("TestProviderList1"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => TestTopPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("TestTopProvider"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.of(context).pushNamed(RoutesName.ROUTE_TEST_PAGE,
              arguments: RouteParams(
                  param1: "lzj", param2: 122, param3: 12.5, mapParam: {"1": 1, "2": 1})),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("route test"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => TestFutureBuild())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("test future build~"),
          ),
        ),
        InkWell(
          onTap: () =>
              Navigator.push(context, CupertinoPageRoute(builder: (context) => TestMusicVibrate())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("响铃及震动"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => InheritedWidgetTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("InheritedWidgetTest"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => CustomChatsTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("custom charts test"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(
              context, CupertinoPageRoute(builder: (context) => ScrollOffsetTestPage())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("scroll offset test"),
          ),
        ),
        InkWell(
          onTap: () => Navigator.push(context, FadeRoute(page: NotifierTestPages())),
          child: ListTile(
            leading: Icon(Icons.title),
            title: Text("notifiers test"),
          ),
        ),
      ],
    );
  }
}
